package cn.lingyangwl.agile.pay.client.controller;

import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.agile.common.security.annotation.Anonymous;
import cn.lingyangwl.agile.log.OperateLog;
import cn.lingyangwl.agile.pay.client.model.rqrs.ConfirmOrderRQ;
import cn.lingyangwl.agile.pay.client.model.rqrs.ConfirmOrderRS;
import cn.lingyangwl.agile.pay.client.model.rqrs.GoodsRefundRQ;
import cn.lingyangwl.agile.pay.client.service.GoodsOrderService;
import cn.lingyangwl.agile.pay.model.rqrs.pay.payorder.PayOrderNotify;
import cn.lingyangwl.agile.pay.model.rqrs.pay.refund.RefundOrderNotify;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@RestController
@RequestMapping("/goodsOrder")
@Anonymous
public class GoodsOrderController {
    @Resource
    private GoodsOrderService goodsOrderService;

    /**
     * 下单
     */
    @PostMapping("/confirmOrder")
    public Resp<ConfirmOrderRS> confirmOrder(@RequestBody @Validated ConfirmOrderRQ req) {
        ConfirmOrderRS confirmOrderRS = goodsOrderService.confirmOrder(req);
        return Resp.ok(confirmOrderRS);
    }

    /**
     * 接着支付
     */
    @PostMapping("/continuePay")
    public Resp<ConfirmOrderRS> continuePay(@RequestParam(value = "mchOrderNo") String mchOrderNo) {
        ConfirmOrderRS confirmOrderRS = goodsOrderService.continuePay(mchOrderNo);
        return Resp.ok(confirmOrderRS);
    }

    /**
     * 退款-全额退款
     */
    @PostMapping("/refund")
    public Resp<?> refund(@RequestBody GoodsRefundRQ rq) {
        goodsOrderService.refund(rq);
        return Resp.ok();
    }

    /**
     * 关闭订单
     */
    @PostMapping("/closeOrder")
    public Resp<?> closeOrder(@RequestParam(value = "mchOrderNo") String mchOrderNo) {
        goodsOrderService.closeGoodsOrder(mchOrderNo);
        return Resp.ok();
    }

    /**
     * 支付结果通知
     */
    @AccessLog(isSaveDb = false)
    @PostMapping("/payNotify")
    public String payNotify(PayOrderNotify req) {
        goodsOrderService.payNotify(req);
        return "SUCCESS";
    }

    /**
     * 支付成功通知
     */
    @OperateLog(isSaveDb = false)
    @PostMapping("/refundNotify")
    public String refundNotify(RefundOrderNotify req) {
        goodsOrderService.refundNotify(req);
        return "SUCCESS";
    }
}
